package com.example.springboot.config.server;

import com.example.springboot.filter.DataSourceFilter;
import com.example.springboot.filter.MyFilter;
import com.example.springboot.filter.RepeatBodyFilter;
import com.example.springboot.listener.MyListener;
import com.example.springboot.servlet.MyServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.Arrays;
import java.util.Map;

/**
 * server的配置类
 *
 * @Author:ChenZhangKun
 * @Date: 2020/9/27 20:34
 */
@Configuration
public class MyServerConfig {

    /**
     * 三大组件的注入
     * 注入servlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(), "/servlet");
        return servletRegistrationBean;
    }

    /**
     * 注入过滤器filter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean myFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        // 注入拦截的servlet
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/servlet"));
        return filterRegistrationBean;
    }

    /**
     * 注册数据源拦截器
     *
     * @return
     */
/*    @Bean
    public FilterRegistrationBean mysqlFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new DataSourceFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/mysql/*"));
        return filterRegistrationBean;
    }*/

    @Bean
    public FilterRegistrationBean repeatBodyFilterRegistrationBean(){
        FilterRegistrationBean<RepeatBodyFilter> filterRegistrationBean=new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new RepeatBodyFilter());
        filterRegistrationBean.addUrlPatterns("/czk/repeat/*");
        return filterRegistrationBean;
    }
    /**
     * 注入监听器
     *
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean<MyListener> servletListener = new ServletListenerRegistrationBean<>(new MyListener());
        return servletListener;

    }

    /**
     * 编写嵌入式servlet容器
     *
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer() {
            @Override
            public void customize(WebServerFactory factory) {
                TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) factory;
                tomcatServletWebServerFactory.setPort(8083);
                // 定制web容器
                // 修改默认的jessionId
                Session.Cookie cookie = tomcatServletWebServerFactory.getSession().getCookie();
                cookie.setName("CzkjessionId");
                // 设置持久时间
                cookie.setMaxAge(Duration.ofHours(5));
                // 定制初始化参数
                Map<String, String> initParameters = tomcatServletWebServerFactory.getInitParameters();
                initParameters.put("czk", "nzj");
                tomcatServletWebServerFactory.setInitParameters(initParameters);
            }
        };
    }
}
